import pytest
from typing import Generator, Dict, Any
from .base import BaseTest
from .data_manager import TestDataManager
from .logger import test_logger
from app.config.settings import settings

class TestFixtures:
    """测试夹具类"""
    
    @staticmethod
    @pytest.fixture(scope="session")
    def base_test() -> Generator[BaseTest, None, None]:
        """基础测试实例夹具"""
        test = BaseTest(f"http://localhost:8000{settings.API_PREFIX}")
        test.setup()
        yield test
        test.teardown()
    
    @staticmethod
    @pytest.fixture(scope="session")
    def data_manager() -> TestDataManager:
        """数据管理器夹具"""
        return TestDataManager()
    
    @staticmethod
    @pytest.fixture(scope="session")
    def auth_token(base_test: BaseTest) -> str:
        """认证token夹具"""
        response = base_test.execute_request(
            "POST",
            "/auth/token",
            json={
                "username": "testuser",
                "password": "testpassword123"
            }
        )
        assert response.status_code == 200
        token = response.json()["access_token"]
        test_logger.test_logger.info("Successfully obtained auth token")
        return token
    
    @staticmethod
    @pytest.fixture(scope="session")
    def auth_headers(auth_token: str) -> Dict[str, str]:
        """认证头夹具"""
        return {"Authorization": f"Bearer {auth_token}"}
    
    @staticmethod
    @pytest.fixture(scope="function")
    def clean_test_data(data_manager: TestDataManager):
        """清理测试数据夹具"""
        yield
        data_manager.clear_data()

# 创建夹具实例
test_fixtures = TestFixtures() 